/**
 * Copyright (C) 2011-20077
 * 设计：		Zhangsk
 * 创建时间：	2011-11-06
 * Flash视频会议核心库 登陆控制器类
 *
 * 
 */

package com.treemon.meetingcore.session
{
    import com.treemon.meetingcore.event.MeetingEvent;
    
    import mx.utils.StringUtil;
    //import flash.events.IEventDispatcher;	
    
    {
        trace("LoginControl.as begin");
    }
    public class LoginControl extends ControlBase
    {
        public function LoginControl(aSession: MeetingSession)
        {
            super(aSession);
        }
    	
    	override protected function addListener(): void
        {
            _session.addEventListener(MeetingEvent.Auth, handleAuth);
            _session.addEventListener(MeetingEvent.AuthOK, handleAuthOK);
            _session.addEventListener(MeetingEvent.Agents, handleAgents);
            _session.addEventListener(MeetingEvent.MeetingEnter, handleMeetingEnter);
            _session.addEventListener(MeetingEvent.User, handleUser);
            _session.addEventListener(MeetingEvent.MMCreate, handleMMCreate);
            _session.addEventListener(MeetingEvent.MMTransport, handleMMTransport);
            _session.addEventListener(MeetingEvent.MeetingRoomAttribute, handleMeetingRoomAttribute);
        }
    	
        private function handleAuth(aEvent: MeetingEvent): void 
    	{
            trace('LoginControl.handleAuth');
            //todo resource digest
            var s: String;
            s = '<iq id="jcl_2" type="set"><query xmlns="jabber:iq:auth"><username>guest</username><resource>{0}</resource><digest>afdc14b797a0ff2ecc8a3d5e882d18a54a0b3e6a</digest></query></iq>';        	 
            _session.sendXML(mx.utils.StringUtil.substitute(s, _session.lastGUID));
            //{0A52F94F-CCCB-4554-8A5C-BAB710121172}
    	}
    	
        private function handleAuthOK(aEvent: MeetingEvent): void
        {
            trace('LoginControl.handleAuthOK');
            //_session.sendXML( '<iq id="jcl_3" to="192.168.1.111" type="get"><query xmlns="jabber:iq:agents"/></iq>' );
            //_session.sendXML( '<presence to="5199@flashgw_1.machine1.com"><x xmlns="meeting:conf:enter" /></presence>' );
            //_session.sendXML( '<iq id="jcl_4" to="5199@flashgw_1.machine1.com" type="set"><query xmlns="meeting:conf:enter"><password>111111</password><nick>zhangsk</nick><username>guest</username><userpass>guest</userpass><lastGUID>{7C8078C8-F56F-40CF-89EA-8B4A9D481656}</lastGUID></query></iq>' );
            //_session.sendXML( '<iq id="jcl_5" type="get"><query xmlns="jabber:iq:private"><storage xmlns="storage:imprefs"/></query></iq>' );
            
            var s: String;
            
            s = '<iq id="{0}" to="{1}" type="get"><query xmlns="jabber:iq:agents"/></iq>';        	 
            _session.sendXML(mx.utils.StringUtil.substitute(s, _session.getGenerateID(), _session.meetingDomain));
                    	
            s = '<presence to="{0}"><x xmlns="meeting:conf:enter" /></presence>';
            _session.sendXML(mx.utils.StringUtil.substitute(s, _session.meetingID));
            
            s = '<iq id="{0}" to="{1}" type="set"><query xmlns="meeting:conf:enter"><password>{2}</password><nick>{3}</nick><username>guest</username><userpass>guest</userpass><lastGUID>{4}</lastGUID><usertype>flash</usertype></query></iq>';
            _session.sendXML(mx.utils.StringUtil.substitute(s, _session.getGenerateID(), _session.meetingID, _session.meetingPassword, _session.meetingUserName, _session.lastGUID));
            //{7C8078C8-F56F-40CF-89EA-8B4A9D481656}
            //todo username-->nick
            
            
            s = '<iq id="{0}" type="get"><query xmlns="jabber:iq:private"><storage xmlns="storage:imprefs"/></query></iq>';
            _session.sendXML(mx.utils.StringUtil.substitute(s, _session.getGenerateID()));
        }
    	
        private function handleAgents(aEvent: MeetingEvent): void
        {
            /*
            <iq id="fcl_2" to="guest@masterjadc2s_1.machine1.com/{0A52F94F-CCCB-4554-8A5C-BAB710121172}" type="result">
              <query xmlns="jabber:iq:agents">
                <agent jid="http://192.168.1.128:80/">
                  <name>Web Server</name>
                  <service>httpurl</service>
                  <MasterIP>192.168.1.128</MasterIP>
                  <Port>80</Port>
                  <TunnelingPort>443</TunnelingPort>
                  <ContextPath>http://192.168.1.128:80/Meeting</ContextPath>
                </agent>
                <agent jid="slavefs_1.machine1.com">
                  <name>slavefs_1.machine1.com</name>
                  <service>slavefs_1.machine1.com</service>
                </agent>
              </query>
            </iq>			
            */
            trace(aEvent.xml.toXMLString());
            var xmlQuery: XML = aEvent.xml.children()[0];
            var ns: Namespace = xmlQuery.namespace();		
            for each (var xmlAgent: XML in xmlQuery.ns::agent)
            {
                trace(xmlAgent.toXMLString());
                var strService: String = xmlAgent.ns::service;
                trace(strService);
                if ("httpurl" == strService)
                {
                    //Todo 会议主从服务器地址，tunneling端口，管理系统上下文路径
                }
                else
                {
                    _session.slaveFS = strService;
                }
            }
        }
		
        private function handleMeetingEnter(aEvent: MeetingEvent): void
        {
            var xmlQuery: XML = aEvent.xml.children()[0];
            var ns: Namespace = xmlQuery.namespace();
            //trace(xmlQuery.ns::id);
            _session.myJID = xmlQuery.ns::id;
            
            trace( xmlQuery.ns::fmsip );
            _session.fmsIP = xmlQuery.ns::fmsip;
            
        }
        
        /**
        * 用户进入，包括自己
        * 
        */
        private function handleUser(aEvent: MeetingEvent): void
        {
        	var xmlQuery: XML = aEvent.xml.children()[0];
        	var ns: Namespace = xmlQuery.namespace();
        	trace(xmlQuery.ns::user.@jid);
        	//_session.myJID = xmlQuery.ns::user.@jid;
        }
		
    	private function handleMMCreate(aEvent: MeetingEvent): void
    	{
            var s: String;
            s = '<iq id="{0}" to="{1}" type="get"><query xmlns="meeting:conf:wannaTransportMM"><protDes><UDP/><HTTP/></protDes></query></iq>';
            _session.sendXML(mx.utils.StringUtil.substitute(s, _session.getGenerateID(), _session.meetingID));	
    	}
    	
		private function handleMMTransport(aEvent: MeetingEvent): void
		{
			var xmlQuery: XML = aEvent.xml.children()[0];
			var ns: Namespace = xmlQuery.namespace();
			_session.myMMID = xmlQuery.ns::protDes.@mmid;
			
            var xmlIQ: XML = //<iq id="" to="" type="set"><query xmlns="meeting:conf:capability"><user jid=""><vSend>disable</vSend><aSend>disable</aSend><vRecv>enable</vRecv><camctrl>disable</camctrl><aRecv>enable</aRecv></user></query></iq>
                <iq id="" to="" type="set">
                    <query xmlns="meeting:conf:capability">
                        <user jid="">
                            <vSend>disable</vSend>
                            <aSend>disable</aSend>
                            <vRecv>enable</vRecv>
                            <camctrl>disable</camctrl>
                            <aRecv>enable</aRecv>
                        </user>
					</query>
                </iq>
            
            
            xmlIQ.@id = _session.getGenerateID();
            xmlIQ.@to = _session.meetingID;
            xmlQuery = xmlIQ.children()[0];
            ns = xmlQuery.namespace();
            xmlQuery.ns::user.@jid = _session.myJID; //"530345@slavemcu_1.machine1.com/&lt;zhangsk&gt;";          
            trace(xmlIQ.toString());
            _session.sendXML(xmlIQ.toString());            
            
            xmlIQ =
                <iq id="" to="" type="set">
                    <query xmlns="meeting:conf:testOver">
                        <audio trnsType="HTTP"/>
                        <video trnsType="UDP"><bw>0</bw><maxudplen>0</maxudplen><rtt>0</rtt></video>
                        <userDes mmid=""/>
                    </query>
                </iq>
            xmlQuery = xmlIQ.children()[0];
            ns = xmlQuery.namespace();
            xmlIQ.@id = _session.getGenerateID();
            xmlIQ.@to = _session.meetingID;
            xmlQuery.ns::userDes.@mmid = _session.myMMID;
            trace(xmlIQ.toString());
            _session.sendXML(xmlIQ.toString());
    	}
    	
    	private function handleMeetingRoomAttribute(aEvent: MeetingEvent): void
    	{
    		//todo 会议室属性，界面层需要控制状态的时候添加
    		var s: String;
    		//s = '<iq id="{0}" to="{1}" type="get"><query xmlns="meeting:conf:wannaTransportMM"><protDes><UDP/><HTTP/></protDes></query></iq>';
    		//_session.sendXML(mx.utils.StringUtil.substitute(s, _session.generateID(), _session.meetingDomain));
    		s = '<iq id="{0}" type="get" to="{1}"><query xmlns="meeting:share:browse"/></iq>'
    		_session.sendXML(mx.utils.StringUtil.substitute(s, _session.getGenerateID(), _session.slaveFS));
    	}
    }
}